package edu.sctu.zhengsenwen.month201506.day20150624.json;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * Created by user15 on 2015/6/24.
 */
public class HttpRequest {


    public String sendGet(String path, String param) {

        String result = null;
        BufferedReader input = null;

        try {
            URL url = new URL(path);

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            httpURLConnection.setRequestProperty("accept", "*/*");
            httpURLConnection.setRequestProperty("connection", "Keep-Alive");
            httpURLConnection.setRequestProperty("user-agent", "Mozilla/4.0 " +
                    "(compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            httpURLConnection.connect();

            Map<String, List<String>> map = httpURLConnection.getHeaderFields();

            for (String key : map.keySet()) {
                System.out.println(key + "-->" + map.get(key));
            }

            input = new BufferedReader(new InputStreamReader
                    (httpURLConnection.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }

        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }


    public String sendPost(String path, String param) {

        PrintWriter output = null;
        BufferedReader input = null;
        String result = "";
        try {
            URL url = new URL(path);

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            httpURLConnection.setRequestProperty("accept", "*/*");
            httpURLConnection.setRequestProperty("connection", "Keep-Alive");
            httpURLConnection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            output = new PrintWriter(httpURLConnection.getOutputStream());
            output.print(param);
            output.flush();
            input = new BufferedReader(
                    new InputStreamReader(httpURLConnection.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (output != null) {
                    output.close();
                }
                if (input != null) {
                    input.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }


    public static void main(String[] args) throws IOException {


        HttpRequest httpRequest = new HttpRequest();
//        String string1 = httpRequest.sendGet("http://api.ii.com.cn/goods/apprec", "");

        String string2 = httpRequest.sendPost("http://api.ii.com.cn/goods/apprec", "");
        System.out.println(string2);
    }
}
